package com.zhangyj.db;

import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.net.URL;
import java.sql.Array;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.Date;
import java.sql.NClob;
import java.sql.ParameterMetaData;
import java.sql.PreparedStatement;
import java.sql.Ref;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.RowId;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

public class NamedPreparedStatementImpl implements NamedPreparedStatement {
	private static Pattern namedPattern = Pattern.compile(":[a-zA-Z\\_\\#\\@]+[a-zA-Z0-9\\#\\@\\_\\.]*");
	private PreparedStatement ps = null;
	private Map<String, List<Integer>> indexMap = new HashMap<String, List<Integer>>();

	NamedPreparedStatementImpl(Connection connection, String sql) throws SQLException {
		if (sql != null && sql.indexOf(':') >= 0) {
			java.util.regex.Matcher m = namedPattern.matcher(sql);
			StringBuffer preparedSql = new StringBuffer();
			int paramIndex = 1;

			while (m.find()) {
				String paramName = sql.substring(m.start() + 1, m.end()).toLowerCase();
				m.appendReplacement(preparedSql, "?");
				List<Integer> indexList = indexMap.get(paramName);
				if (indexList == null) {
					indexList = new ArrayList<Integer>();
					indexMap.put(paramName.toLowerCase(), indexList);
				}
				indexList.add(new Integer(paramIndex++));
			}
			m.appendTail(preparedSql);
			sql = preparedSql.toString();
		}

		ps = connection.prepareStatement(sql);
	}

	public Set<String> getParameterNames() {
		return indexMap.keySet();
	}

	private List<Integer> getIndexListByName(String parameterName) throws SQLException {
		List<Integer> indexList = indexMap.get(parameterName.toLowerCase());
		if (indexList == null) {
			throw new SQLException("invalid parameter name :" + parameterName + "");
		}
		return indexList;
	}

	public void setArray(String parameterName, Array x) throws SQLException {
		List<Integer> indexList = getIndexListByName(parameterName);
		for (Iterator<Integer> itr = indexList.iterator(); itr.hasNext();) {
			Integer index = itr.next();
			setArray(index.intValue(), x);
		}
	}

	public void setAsciiStream(String parameterName, InputStream x, int length) throws SQLException {
		List<Integer> indexList = getIndexListByName(parameterName);
		for (Iterator<Integer> itr = indexList.iterator(); itr.hasNext();) {
			Integer index = itr.next();
			setAsciiStream(index.intValue(), x, length);
		}
	}

	public void setBigDecimal(String parameterName, BigDecimal x) throws SQLException {
		List<Integer> indexList = getIndexListByName(parameterName);
		for (Iterator<Integer> itr = indexList.iterator(); itr.hasNext();) {
			Integer index = itr.next();
			setBigDecimal(index.intValue(), x);
		}
	}

	public void setBinaryStream(String parameterName, InputStream x, int length) throws SQLException {
		List<Integer> indexList = getIndexListByName(parameterName);
		for (Iterator<Integer> itr = indexList.iterator(); itr.hasNext();) {
			Integer index = itr.next();
			setBinaryStream(index.intValue(), x, length);
		}
	}

	public void setBlob(String parameterName, Blob x) throws SQLException {
		List<Integer> indexList = getIndexListByName(parameterName);
		for (Iterator<Integer> itr = indexList.iterator(); itr.hasNext();) {
			Integer index = itr.next();
			setBlob(index.intValue(), x);
		}
	}

	public void setBoolean(String parameterName, boolean x) throws SQLException {
		List<Integer> indexList = getIndexListByName(parameterName);
		for (Iterator<Integer> itr = indexList.iterator(); itr.hasNext();) {
			Integer index = itr.next();
			setBoolean(index.intValue(), x);
		}
	}

	public void setByte(String parameterName, byte x) throws SQLException {
		List<Integer> indexList = getIndexListByName(parameterName);
		for (Iterator<Integer> itr = indexList.iterator(); itr.hasNext();) {
			Integer index = itr.next();
			setByte(index.intValue(), x);
		}
	}

	public void setBytes(String parameterName, byte[] x) throws SQLException {
		List<Integer> indexList = getIndexListByName(parameterName);
		for (Iterator<Integer> itr = indexList.iterator(); itr.hasNext();) {
			Integer index = itr.next();
			setBytes(index.intValue(), x);
		}
	}

	public void setCharacterStream(String parameterName, Reader reader, int length) throws SQLException {
		List<Integer> indexList = getIndexListByName(parameterName);
		for (Iterator<Integer> itr = indexList.iterator(); itr.hasNext();) {
			Integer index = itr.next();
			setCharacterStream(index.intValue(), reader, length);
		}
	}

	public void setClob(String parameterName, Clob x) throws SQLException {
		List<Integer> indexList = getIndexListByName(parameterName);
		for (Iterator<Integer> itr = indexList.iterator(); itr.hasNext();) {
			Integer index = itr.next();
			setClob(index.intValue(), x);
		}
	}

	public void setDate(String parameterName, Date x) throws SQLException {
		List<Integer> indexList = getIndexListByName(parameterName);
		for (Iterator<Integer> itr = indexList.iterator(); itr.hasNext();) {
			Integer index = itr.next();
			setDate(index.intValue(), x);
		}
	}

	public void setDate(String parameterName, Date x, Calendar cal) throws SQLException {
		List<Integer> indexList = getIndexListByName(parameterName);
		for (Iterator<Integer> itr = indexList.iterator(); itr.hasNext();) {
			Integer index = itr.next();
			setDate(index.intValue(), x, cal);
		}
	}

	public void setDouble(String parameterName, double x) throws SQLException {
		List<Integer> indexList = getIndexListByName(parameterName);
		for (Iterator<Integer> itr = indexList.iterator(); itr.hasNext();) {
			Integer index = itr.next();
			setDouble(index.intValue(), x);
		}
	}

	public void setFloat(String parameterName, float x) throws SQLException {
		List<Integer> indexList = getIndexListByName(parameterName);
		for (Iterator<Integer> itr = indexList.iterator(); itr.hasNext();) {
			Integer index = itr.next();
			setFloat(index.intValue(), x);
		}
	}

	public void setInt(String parameterName, int x) throws SQLException {
		List<Integer> indexList = getIndexListByName(parameterName);
		for (Iterator<Integer> itr = indexList.iterator(); itr.hasNext();) {
			Integer index = itr.next();
			setInt(index.intValue(), x);
		}
	}

	public void setLong(String parameterName, long x) throws SQLException {
		List<Integer> indexList = getIndexListByName(parameterName);
		for (Iterator<Integer> itr = indexList.iterator(); itr.hasNext();) {
			Integer index = itr.next();
			setLong(index.intValue(), x);
		}
	}

	public void setNull(String parameterName, int sqlType) throws SQLException {
		List<Integer> indexList = getIndexListByName(parameterName);
		for (Iterator<Integer> itr = indexList.iterator(); itr.hasNext();) {
			Integer index = itr.next();
			setNull(index.intValue(), sqlType);
		}
	}

	public void setNull(String parameterName, int sqlType, String typeName) throws SQLException {
		List<Integer> indexList = getIndexListByName(parameterName);
		for (Iterator<Integer> itr = indexList.iterator(); itr.hasNext();) {
			Integer index = itr.next();
			setNull(index.intValue(), sqlType, typeName);
		}
	}

	public void setObject(String parameterName, Object x, int targetSqlType, int scale) throws SQLException {
		List<Integer> indexList = getIndexListByName(parameterName);
		for (Iterator<Integer> itr = indexList.iterator(); itr.hasNext();) {
			Integer index = itr.next();
			setObject(index.intValue(), x, targetSqlType, scale);
		}
	}

	public void setObject(String parameterName, Object x, int targetSqlType) throws SQLException {
		List<Integer> indexList = getIndexListByName(parameterName);
		for (Iterator<Integer> itr = indexList.iterator(); itr.hasNext();) {
			Integer index = itr.next();
			setObject(index.intValue(), x, targetSqlType);
		}
	}

	public void setObject(String parameterName, Object x) throws SQLException {
		List<Integer> indexList = getIndexListByName(parameterName);
		for (Iterator<Integer> itr = indexList.iterator(); itr.hasNext();) {
			Integer index = itr.next();
			setObject(index.intValue(), x);
		}
	}

	public void setRef(String parameterName, Ref x) throws SQLException {
		List<Integer> indexList = getIndexListByName(parameterName);
		for (Iterator<Integer> itr = indexList.iterator(); itr.hasNext();) {
			Integer index = itr.next();
			setRef(index.intValue(), x);
		}
	}

	public void setShort(String parameterName, short x) throws SQLException {
		List<Integer> indexList = getIndexListByName(parameterName);
		for (Iterator<Integer> itr = indexList.iterator(); itr.hasNext();) {
			Integer index = itr.next();
			setShort(index.intValue(), x);
		}
	}

	public void setString(String parameterName, String x) throws SQLException {
		List<Integer> indexList = getIndexListByName(parameterName);
		for (Iterator<Integer> itr = indexList.iterator(); itr.hasNext();) {
			Integer index = itr.next();
			setString(index.intValue(), x);
		}
	}

	public void setTime(String parameterName, Time x) throws SQLException {
		List<Integer> indexList = getIndexListByName(parameterName);
		for (Iterator<Integer> itr = indexList.iterator(); itr.hasNext();) {
			Integer index = itr.next();
			setTime(index.intValue(), x);
		}
	}

	public void setTime(String parameterName, Time x, Calendar cal) throws SQLException {
		List<Integer> indexList = getIndexListByName(parameterName);
		for (Iterator<Integer> itr = indexList.iterator(); itr.hasNext();) {
			Integer index = itr.next();
			setTime(index.intValue(), x, cal);
		}
	}

	public void setTimestamp(String parameterName, Timestamp x) throws SQLException {
		List<Integer> indexList = getIndexListByName(parameterName);
		for (Iterator<Integer> itr = indexList.iterator(); itr.hasNext();) {
			Integer index = itr.next();
			setTimestamp(index.intValue(), x);
		}
	}

	public void setTimestamp(String parameterName, Timestamp x, Calendar cal) throws SQLException {
		List<Integer> indexList = getIndexListByName(parameterName);
		for (Iterator<Integer> itr = indexList.iterator(); itr.hasNext();) {
			Integer index = itr.next();
			setTimestamp(index.intValue(), x, cal);
		}
	}

	public void setURL(String parameterName, URL x) throws SQLException {
		List<Integer> indexList = getIndexListByName(parameterName);
		for (Iterator<Integer> itr = indexList.iterator(); itr.hasNext();) {
			Integer index = itr.next();
			setURL(index.intValue(), x);
		}
	}

	public void setChar(String parameterName, String x) throws SQLException {
		List<Integer> indexList = getIndexListByName(parameterName);
		for (Iterator<Integer> itr = indexList.iterator(); itr.hasNext();) {
			Integer index = itr.next();
			setChar(index.intValue(), x);
		}
	}

	public void addBatch() throws SQLException {
		ps.addBatch();
	}

	public void clearParameters() throws SQLException {
		ps.clearParameters();
	}

	public boolean execute() throws SQLException {
		return ps.execute();
	}

	public ResultSet executeQuery() throws SQLException {
		return ps.executeQuery();
	}

	public int executeUpdate() throws SQLException {
		return ps.executeUpdate();
	}

	public ResultSetMetaData getMetaData() throws SQLException {
		return ps.getMetaData();
	}

	public ParameterMetaData getParameterMetaData() throws SQLException {
		return ps.getParameterMetaData();
	}

	public void setArray(int i, Array x) throws SQLException {
		ps.setArray(i, x);
	}

	public void setAsciiStream(int parameterIndex, InputStream x, int length) throws SQLException {
		ps.setAsciiStream(parameterIndex, x, length);
	}

	public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException {
		ps.setBigDecimal(parameterIndex, x);
	}

	public void setBinaryStream(int parameterIndex, InputStream x, int length) throws SQLException {
		ps.setBinaryStream(parameterIndex, x, length);
	}

	public void setBlob(int i, Blob x) throws SQLException {
		ps.setBlob(i, x);
	}

	public void setBoolean(int parameterIndex, boolean x) throws SQLException {
		ps.setBoolean(parameterIndex, x);
	}

	public void setByte(int parameterIndex, byte x) throws SQLException {
		ps.setByte(parameterIndex, x);
	}

	public void setBytes(int parameterIndex, byte[] x) throws SQLException {
		ps.setBytes(parameterIndex, x);
	}

	public void setCharacterStream(int parameterIndex, Reader reader, int length) throws SQLException {
		ps.setCharacterStream(parameterIndex, reader, length);
	}

	public void setClob(int i, Clob x) throws SQLException {
		ps.setClob(i, x);
	}

	public void setDate(int parameterIndex, Date x) throws SQLException {
		ps.setDate(parameterIndex, x);
	}

	public void setDate(int parameterIndex, Date x, Calendar cal) throws SQLException {
		ps.setDate(parameterIndex, x, cal);
	}

	public void setDouble(int parameterIndex, double x) throws SQLException {
		ps.setDouble(parameterIndex, x);
	}

	public void setFloat(int parameterIndex, float x) throws SQLException {
		ps.setFloat(parameterIndex, x);
	}

	public void setInt(int parameterIndex, int x) throws SQLException {
		ps.setInt(parameterIndex, x);
	}

	public void setLong(int parameterIndex, long x) throws SQLException {
		ps.setLong(parameterIndex, x);
	}

	public void setNull(int parameterIndex, int sqlType) throws SQLException {
		ps.setNull(parameterIndex, sqlType);
	}

	public void setNull(int paramIndex, int sqlType, String typeName) throws SQLException {
		ps.setNull(paramIndex, sqlType, typeName);
	}

	public void setObject(int parameterIndex, Object x) throws SQLException {
		ps.setObject(parameterIndex, x);
	}

	public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQLException {
		ps.setObject(parameterIndex, x, targetSqlType);
	}

	public void setObject(int parameterIndex, Object x, int targetSqlType, int scale) throws SQLException {
		ps.setObject(parameterIndex, x, targetSqlType, scale);
	}

	public void setRef(int i, Ref x) throws SQLException {
		ps.setRef(i, x);
	}

	public void setShort(int parameterIndex, short x) throws SQLException {
		ps.setShort(parameterIndex, x);
	}

	public void setString(int parameterIndex, String x) throws SQLException {
		ps.setString(parameterIndex, x);
	}

	public void setTime(int parameterIndex, Time x) throws SQLException {
		ps.setTime(parameterIndex, x);
	}

	public void setTime(int parameterIndex, Time x, Calendar cal) throws SQLException {
		ps.setTime(parameterIndex, x, cal);
	}

	public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException {
		ps.setTimestamp(parameterIndex, x);
	}

	public void setTimestamp(int parameterIndex, Timestamp x, Calendar cal) throws SQLException {
		ps.setTimestamp(parameterIndex, x, cal);
	}

	public void setURL(int parameterIndex, URL x) throws SQLException {
		ps.setURL(parameterIndex, x);
	}

	@SuppressWarnings("deprecation")
	public void setUnicodeStream(int parameterIndex, InputStream x, int length) throws SQLException {
		ps.setUnicodeStream(parameterIndex, x, length);
	}

	public void setChar(int parameterIndex, String x) throws SQLException {
		setString(parameterIndex, x);
	}

	public void addBatch(String sql) throws SQLException {
		ps.addBatch(sql);
	}

	public void cancel() throws SQLException {
		ps.cancel();
	}

	public void clearBatch() throws SQLException {
		ps.clearBatch();
	}

	public void clearWarnings() throws SQLException {
		ps.clearWarnings();
	}

	public void close() throws SQLException {
		indexMap.clear();
		indexMap = null;

		ps.close();
		ps = null;
	}

	public boolean execute(String sql) throws SQLException {
		return ps.execute(sql);
	}

	public boolean execute(String sql, int autoGeneratedKeys) throws SQLException {
		return ps.execute(sql, autoGeneratedKeys);
	}

	public boolean execute(String sql, int[] columnIndexes) throws SQLException {
		return ps.execute(sql, columnIndexes);
	}

	public boolean execute(String sql, String[] columnNames) throws SQLException {
		return ps.execute(sql, columnNames);
	}

	public int[] executeBatch() throws SQLException {
		return ps.executeBatch();
	}

	public ResultSet executeQuery(String sql) throws SQLException {
		return ps.executeQuery(sql);
	}

	public int executeUpdate(String sql) throws SQLException {
		return ps.executeUpdate(sql);
	}

	public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException {
		return ps.executeUpdate(sql, autoGeneratedKeys);
	}

	public int executeUpdate(String sql, int[] columnIndexes) throws SQLException {
		return ps.executeUpdate(sql, columnIndexes);
	}

	public int executeUpdate(String sql, String[] columnNames) throws SQLException {
		return ps.executeUpdate(sql, columnNames);
	}

	public Connection getConnection() throws SQLException {
		return ps.getConnection();
	}

	public int getFetchDirection() throws SQLException {
		return ps.getFetchDirection();
	}

	public int getFetchSize() throws SQLException {
		return ps.getFetchSize();
	}

	public ResultSet getGeneratedKeys() throws SQLException {
		return ps.getGeneratedKeys();
	}

	public int getMaxFieldSize() throws SQLException {
		return ps.getMaxFieldSize();
	}

	public int getMaxRows() throws SQLException {
		return ps.getMaxRows();
	}

	public boolean getMoreResults() throws SQLException {
		return ps.getMoreResults();
	}

	public boolean getMoreResults(int current) throws SQLException {
		return ps.getMoreResults(current);
	}

	public int getQueryTimeout() throws SQLException {
		return ps.getQueryTimeout();
	}

	public ResultSet getResultSet() throws SQLException {
		return ps.getResultSet();
	}

	public int getResultSetConcurrency() throws SQLException {
		return ps.getResultSetConcurrency();
	}

	public int getResultSetHoldability() throws SQLException {
		return ps.getResultSetHoldability();
	}

	public int getResultSetType() throws SQLException {
		return ps.getResultSetType();
	}

	public int getUpdateCount() throws SQLException {
		return ps.getUpdateCount();
	}

	public SQLWarning getWarnings() throws SQLException {
		return ps.getWarnings();
	}

	public void setCursorName(String name) throws SQLException {
		ps.setCursorName(name);
	}

	public void setEscapeProcessing(boolean enable) throws SQLException {
		ps.setEscapeProcessing(enable);
	}

	public void setFetchDirection(int direction) throws SQLException {
		ps.setFetchDirection(direction);
	}

	public void setFetchSize(int rows) throws SQLException {
		ps.setFetchSize(rows);
	}

	public void setMaxFieldSize(int max) throws SQLException {
		ps.setMaxFieldSize(max);
	}

	public void setMaxRows(int max) throws SQLException {
		ps.setMaxRows(max);
	}

	public void setQueryTimeout(int seconds) throws SQLException {
		ps.setQueryTimeout(seconds);
	}

	
	public void setAsciiStream(int parameterIndex, InputStream x) throws SQLException {
		ps.setAsciiStream(parameterIndex, x);
	}

	
	public void setAsciiStream(int parameterIndex, InputStream x, long length) throws SQLException {
		ps.setAsciiStream(parameterIndex, x, length);
	}

	
	public void setBinaryStream(int parameterIndex, InputStream x) throws SQLException {
		ps.setBinaryStream(parameterIndex, x);
	}

	
	public void setBinaryStream(int parameterIndex, InputStream x, long length) throws SQLException {
		ps.setBinaryStream(parameterIndex, x, length);
	}

	
	public void setBlob(int parameterIndex, InputStream inputStream) throws SQLException {
		ps.setBlob(parameterIndex, inputStream);
	}

	
	public void setBlob(int parameterIndex, InputStream inputStream, long length) throws SQLException {
		ps.setBlob(parameterIndex, inputStream, length);
	}

	
	public void setCharacterStream(int parameterIndex, Reader reader) throws SQLException {
		ps.setCharacterStream(parameterIndex, reader);
	}

	
	public void setCharacterStream(int parameterIndex, Reader reader, long length) throws SQLException {
		ps.setCharacterStream(parameterIndex, reader, length);
	}

	
	public void setClob(int parameterIndex, Reader reader) throws SQLException {
		ps.setClob(parameterIndex, reader);
	}

	
	public void setClob(int parameterIndex, Reader reader, long length) throws SQLException {
		ps.setClob(parameterIndex, reader, length);
	}

	
	public void setNCharacterStream(int parameterIndex, Reader value) throws SQLException {
		ps.setNCharacterStream(parameterIndex, value);
	}

	
	public void setNCharacterStream(int parameterIndex, Reader value, long length) throws SQLException {
		ps.setNCharacterStream(parameterIndex, value, length);
	}

	
	public void setNClob(int parameterIndex, NClob value) throws SQLException {
		ps.setNClob(parameterIndex, value);
	}

	
	public void setNClob(int parameterIndex, Reader reader) throws SQLException {
		ps.setNClob(parameterIndex, reader);
	}

	
	public void setNClob(int parameterIndex, Reader reader, long length) throws SQLException {
		ps.setNClob(parameterIndex, reader, length);
	}

	
	public void setNString(int parameterIndex, String value) throws SQLException {
		ps.setNString(parameterIndex, value);
	}

	
	public void setRowId(int parameterIndex, RowId x) throws SQLException {
		ps.setRowId(parameterIndex, x);
	}

	
	public void setSQLXML(int parameterIndex, SQLXML xmlObject) throws SQLException {
		ps.setSQLXML(parameterIndex, xmlObject);
	}

	
	public boolean isClosed() throws SQLException {
		return ps.isClosed();
	}

	
	public boolean isPoolable() throws SQLException {
		return ps.isPoolable();
	}

	
	public void setPoolable(boolean poolable) throws SQLException {
		ps.setPoolable(poolable);
	}

	
	public boolean isWrapperFor(Class<?> iface) throws SQLException {
		return ps.isWrapperFor(iface);
	}

	
	public <T> T unwrap(Class<T> iface) throws SQLException {
		return ps.unwrap(iface);
	}
}
